<?php

class Http
{

    public static function sendByAsync($url, $send_str = '', $header_arr = [])
    {

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $send_str,
            CURLOPT_HTTPHEADER => $header_arr,
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) {
            return "cURL Error #:" . $err;
        } else {
            return $response;
        }
    }

    public static function request($url, $dir, $file, $token = false)
    {
        $files = realpath($dir . '/' . $file);
        // 文件名称
        $file_name = basename($files);
        // 文件内容
        $content = file_get_contents($files);
        // 生成MD5序列，必须
        $md5 = md5_file($files);

        if (!$token) {
            $token = 'syjcx';
        }
        $header_arr = [
            'binfile-md5:  ' . $md5,
            'binfile-auth: ' . $token,
            'binfile-gzip: false',
            'Content-Type: multipart/form-data; boundary=syjcxguangdong'
        ];

        $sendStr = "--syjcxguangdong
Content-Disposition: form-data; name=\"binFile\"; filename=\"$file_name\"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary\r\n
$content
--syjcxguangdong
Content-Disposition: form-data; name=\"filename\"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit\r\n
$file_name\r\n
--syjcxguangdong--";

echo $content;

        return self::sendByAsync($url, $sendStr, $header_arr);
    }
}

// 开始执行
define('APP_ROOT', __DIR__);
$dir = APP_ROOT . "/tmp";
$config = include APP_ROOT . '/../config/config.php';
$token = false;
$handle = opendir($dir);
while (false !== $file = (readdir($handle))) {
    if ($file != '.' && $file != '..') {
        if (strpos($file, 'TOKEN_ACCESS')) {
            $files = realpath($dir . '/' . $file);
            if (file_exists($files)) {
                $result = Http::request($config['api_gd_url'], $dir, $file, $token);
                $token = json_decode($result, true);
            }
        }
    }
}
echo '-----';
var_dump( $token);
closedir($handle);
$new_handle = opendir($dir);
if ($token) {
    while (false !== $new_file = (readdir($new_handle))) {
        if ($new_file != '.' && $new_file != '..') {
            $files = realpath($dir . '/' . $new_file);
            // if (strpos($new_file, 'EVALUATE_CKTSCL')) {
            //     if (file_exists($files)) {
            //         $result = Http::request($config['api_gd_url'], $dir, $new_file, $token['token']);
            //         echo $result . '<br/>';
            //     }
            // }
            // echo basename($files).'<br/>';
            if (!strpos($new_file, 'TOKEN_ACCESS')) {
                if (file_exists($files)) {
                    $result = Http::request($config['api_gd_url'], $dir, $new_file, $token['token']);
                    echo $result . '<br/>';
                }
            }

        }
    }
}
closedir($new_handle);
